<?php
/**
 * User: Netman
 * Date: 14-3-27
 * Time: 下午9:53
 */

class Stourweb_Controller extends Class_Controller {

    protected $cmsurl;
    protected $platform_no=5;

    /*
     * before
     */
    public function before()
    {
        parent::before();
        $this->cmsurl = URL::site();
        //常用变量赋值
        $this->assign('cmsurl', $this->cmsurl);
        $this->assign('member', Common::session('member'));
        $this->assign('webname', $GLOBALS['cfg_webname']);
        $this->assign('defaultimg', $this->cmsurl . 'public/images/grey.gif');
        $params = $this->request->param('params');
        $this->params = $this->analyze_param($params);
        $controller = $this->request->controller();
        $action = $this->request->action();
        $directory = $this->request->directory();
        //  $_POST = Common::remove_xss($_POST);
        //  $_GET = Common::remove_xss($_GET);
        //  $_COOKIE = Common::remove_xss($_COOKIE);
        // $_REQUEST = Common::remove_xss($_REQUEST);
        $envelope_order_id = 0;
        if($controller=='envelope'&&$action=='view')
        {
            $envelope_order_id = intval(base64_decode($this->params['params'])) ;

        }

        $client_ip =  St_Client::get_ip();

        Common::before_header($controller, $action,$envelope_order_id);
        if($GLOBALS['cfg_force_login']==1 && $directory!='member'
            && !in_array($controller,array('pub','city','fenxiao','common','error'))
            && St_Functions::is_url_need_verify()
            && $client_ip!=$_SERVER['SERVER_ADDR'])
        {
            //file_put_contents('bbbb.txt',$controller,FILE_APPEND);
            $member = Model_Member_Login::check_login_info();
            if(empty($member['mid']))
            {
                $this->request->redirect('member/login');
            }
        }

        if($GLOBALS['cfg_member_force_verify']==1
            && !in_array($controller,array('pub','city','common','destination','member','sign','error'))  //公共页面不能跳
            && strpos($action,'ajax_')===false  //ajax请求不能跳
            && $directory!='member'
            && St_Functions::is_url_need_verify()
            && $client_ip!=$_SERVER['SERVER_ADDR']
        )  //会员页面不需要跳
        {

            $member = Model_Member_Login::check_login_info();
            if(!empty($member['mid']))
            {
                $verifystatus = DB::select('verifystatus')->from('member')->where('mid','=',$member['mid'])->execute()->get('verifystatus');
                if($verifystatus!=2)
                {
                    $this->request->redirect('member?force=1#&certification');
                }
            }
        }

    }
    public function display($tpl, $page = null,$user_define_tpl=0)
    {
        $file = null;
        if(!$user_define_tpl)
        {
            //上传模板
            if ($page)
            {
                $query = DB::select('id')->from('page')->where('pagename', '=', $page);
                $query = DB::select()->from('m_page_config')->where('pageid', 'in', $query)->and_where('isuse', '=', 1)->limit(1);
                $result = $query->execute()->current();

                $arr=DB::select('path')
                    ->from('m_page_config')
                    ->where('path', 'like', 'stourwebcms_templet_uni_no%')
                    ->execute()
                    ->as_array();
                $data = array();
                foreach ($arr as $v){
                    array_push($data,$v['path']);
                }
                $arr = $data;
                $result = $query->execute()->current();
                if(!in_array($result['path'],$arr))
                {
                    $file = "usertpl/{$result['path']}/index";
                    //赋值用户模板名称,以便在模板上使用
                    $this->assign("user_tpl",$result['path']);
                }
                else
                {
                    $file =  'default/'.$tpl;
                }

            }
            //系统模板
            if (!file_exists(DOCROOT . $file . EXT))
            {
                //自定义主题
                $file = trim($GLOBALS['cfg_default_templet'], '/') . '/' . $tpl;

                if (!file_exists(APPPATH . $file . EXT))
                {
                    //默认主题
                    $file = 'default/' . $tpl;
                }
            }

        }
        else
        {
            $file =  $tpl;
        }


        //渲染视图
        $view = Stourweb_View::factory($file);
        foreach ($this->_data as $key => $value)
        {
            $view->set($key, $value);
            $view->set_global($key, $value);
        }
        $this->response->body($view->render());
    }
}